<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Booklet extends Controller_DefaultTemplate{

public function action_index()
{
}

public function before()
{
	// Run anything that need ot run before this.
	parent::before();

	if (!Auth::instance()->logged_in())
	{
		$this->request->redirect('/user/login');
	}
}

public function action_list()
{
	$this->template->content = View::factory('book/booklet_list');
	$booklet = ORM::factory('booklet')->order_by('id', 'DESC')->find_all();
	$this->template->content->set('booklet_list', $booklet->as_array());
}

public function action_locklist()
{
	$this->template->content = View::factory('book/booklet_locklist');
	$booklet = ORM::factory('booklet')->order_by('id', 'DESC')->where('lock', '=', 'on')->find_all();
	$this->template->content->set('booklet_list', $booklet->as_array());
}

public function action_unlocklist()
{
	$this->template->content = View::factory('book/booklet_unlocklist');
	$booklet = ORM::factory('booklet')->order_by('id', 'DESC')->where('lock', '=', 'off')->find_all();
	$this->template->content->set('booklet_list', $booklet->as_array());
}

public function action_add()
{
	$this->template->content = View::factory('book/booklet_add');
	$category = ORM::factory('category')->order_by('order', 'DESC')->find_all();
	$this->template->content->set('category_list', $category->as_array());
}

public function action_delete()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$booklet = ORM::factory('booklet')->where('id', '=', $id)->find();
		if ($booklet->loaded())
		{
			$booklet->delete();
		}
		$this->request->redirect();
	}
}

public function action_modify()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$category_list = ORM::factory('category')->find_all();
		$booklet = ORM::factory('booklet')->where('id', '=', $id)->find();
		if ($booklet->loaded())
		{
			$this->template->content = View::factory('book/booklet_modify');
			$this->template->content->set('booklet', $booklet);
			$this->template->content->set('category_list', $category_list);
		}
	}
}

public function action_new()
{
	if ($_POST)
	{
		$category_id_array = arr::get($_POST, 'categorySelect');
		/*
		$image_file_name = Book_Booklet::instance()->save_image();
		if ($image_file_name == NULL)
		{
			echo "未上传图片";
			return;
		}
		 */
		Kohana::log('info', 'Admin logged in successfully');

		$preview =  arr::get($_POST, 'preview');
		$lock =  arr::get($_POST, 'lock');
		Kohana::debug($lock);
		$booklet = ORM::factory('booklet');
		$booklet->values(array(
			'name' 		  => arr::get($_POST, 'name'),
			'description' => arr::get($_POST, 'description'),
			//'image'  	  => $image_file_name,
			'create_time' => time(),
		));
		try
        {
			$booklet->save();
		}
		catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors('model');
			print_r($errors);
			return;
        }
		
		Book_Booklet::instance()->save_booklet_record($booklet);
		Book_Booklet::instance()->save_category($booklet, $category_id_array);
		$this->request->redirect();
		return;
	}
}

public function action_save()
{
	if ($_POST)
    {
		// 保存booklet数据
		$booklet = ORM::factory("booklet")->where("id", "=", arr::get($_POST, "id"))->find();;
		if ($booklet->loaded())
		{
			$booklet->name = arr::get($_POST, 'name');
			$booklet->description = arr::get($_POST, 'description');
			$lock = arr::get($_POST, 'lock');
			if ($lock == "on")
			{
				$booklet->lock = "on";
			}
			else 
			{
				$booklet->lock = "off";
			}
			$booklet->update_time = time();
			try
			{
				$booklet->update();
			}
			catch (ORM_Validation_Exception $e)
			{
				$errors = $e->errors('model');
				print_r($errors);
				return;
			}
			Book_Booklet::instance()->save_category($booklet, arr::get($_POST, 'categorySelect'));

			echo "更新书成功";
			return;
		}
	}
	echo "更新失败";
}





}



